//
//  CYFinanceInvestAmountCell.m
//  cyjf
//
//  Created by tauCross on 2017/9/25.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYFinanceInvestAmountCell.h"

#import <TCNoActionControl.h>



@implementation CYFinanceInvestAmountCellObject

@end



@interface CYFinanceInvestAmountCell ()

@property(nonatomic, strong)CYFinanceInvestAmountCellObject *object;

@property(nonatomic, strong)UILabel *limitLabel;
@property(nonatomic, strong)UIView *limitLine;
@property(nonatomic, strong)UITextField *amountBox;
@property(nonatomic, strong)UIView *amountLine;
@property(nonatomic, strong)UILabel *trialLabel;

@end


@implementation CYFinanceInvestAmountCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.limitLabel = [[UILabel alloc] init];
        self.limitLabel.textColor = HEXCOLOR(@"444444");
        self.limitLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.limitLabel];
        
        self.limitLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.limitLine];
        
        self.amountBox = [[TCNoActionTextField alloc] init];
        self.amountBox.keyboardType = UIKeyboardTypeNumberPad;
        self.amountBox.font = [UIFont systemFontOfSize:30];
        self.amountBox.textColor = HEXCOLOR(@"444444");
        [self.amountBox addTarget:self action:@selector(onAmountBoxEditingChanged) forControlEvents:UIControlEventEditingChanged];
        self.amountBox.leftViewMode = UITextFieldViewModeAlways;
        self.amountBox.leftView = ({
            UILabel *label = [[UILabel alloc] init];
            label.text = @"¥";
            label.textColor = HEXCOLOR(@"444444");
            label.font = [UIFont systemFontOfSize:28 weight:UIFontWeightLight];
            label.textAlignment = NSTextAlignmentCenter;
            label.height = 80;
            label.width = 38;
            label;
        });
        [self.contentView addSubview:self.amountBox];
        
        self.amountLine = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        [self.contentView addSubview:self.amountLine];
        
        self.trialLabel = [[UILabel alloc] init];
        self.trialLabel.textColor = APP_DEFAULT_COLOR_RED;
        self.trialLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.trialLabel];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 160;
}

- (BOOL)shouldUpdateCellWithObject:(id)object
{
    self.object = object;
    
    if(self.object.limit)
    {
        self.limitLabel.text = [NSString stringWithFormat:@"项目可投金额:%@元", [self.object.limit.amountString componentsSeparatedByString:@"."].firstObject];
    }
    else
    {
        self.limitLabel.text = @"";
    }
    if(self.object.start)
    {
        NSMutableParagraphStyle *style = [self.amountBox.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
        if(@available(iOS 11.0, *))
        {
            
        }
        else
        {
            style.minimumLineHeight = self.amountBox.font.lineHeight - (self.amountBox.font.lineHeight - [UIFont systemFontOfSize:18 weight:UIFontWeightRegular].lineHeight) / 2.0;
        }
        self.amountBox.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@元起投", [self.object.start.amountString componentsSeparatedByString:@"."].firstObject] attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18 weight:UIFontWeightRegular], NSForegroundColorAttributeName : HEXCOLOR(@"999999"), NSParagraphStyleAttributeName : style}];
    }
    else
    {
        self.amountBox.attributedPlaceholder = nil;
    }
    [self onAmountBoxEditingChanged];
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.limitLabel sizeToFit];
    self.limitLabel.left = 20;
    self.limitLabel.centerY = 20;
    
    self.limitLine.width = self.contentView.width - 20;
    self.limitLine.height = 1 / SCREEN_SCALE;
    self.limitLine.bottom = 40;
    self.limitLine.right = self.contentView.width;
    
    self.amountBox.width = self.contentView.width - 31;
    self.amountBox.height = 80;
    self.amountBox.top = 40;
    self.amountBox.left = 11;
    
    self.amountLine.width = self.contentView.width - 20;
    self.amountLine.height = 1 / SCREEN_SCALE;
    self.amountLine.bottom = 120;
    self.amountLine.right = self.contentView.width;
    
    self.trialLabel.height = 40;
    self.trialLabel.width = self.contentView.width - 20;
    self.trialLabel.left = 20;
    self.trialLabel.bottom = self.contentView.height;
}

#pragma mark - Control Events
- (void)onAmountBoxEditingChanged
{
    self.object.amount = @([[self.amountBox.text stringByReplacingOccurrencesOfString:@"," withString:@""] integerValue]);
    if(self.object.amount.integerValue > self.object.limit.integerValue)
    {
        self.object.amount = self.object.limit;
    }
    self.amountBox.text = self.object.amount.integerValue > 0 ? [self.object.amount.amountString componentsSeparatedByString:@"."].firstObject : @"";
    CGFloat amount = self.object.amount.doubleValue;
    CGFloat due = self.object.due.doubleValue;
    CGFloat rate = self.object.rate.doubleValue / 100;
    CGFloat interest = amount * (due / CY_YEAR_DAY) * rate;
    NSString *interestString = [NSString stringWithFormat:@"预期收益：%@元", @(interest).amountString];
    self.trialLabel.text = interestString;
}

@end
